package com.sc.admin.base.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sc.admin.base.service.IRouteService;
import com.sc.admin.base.vo.route.RouteListVO;
import com.sc.common.dto.IdsDTO;
import com.sc.common.entity.RouteEntity;
import com.sc.common.response.Response;
import com.sc.core.util.DataAutoSearch.DataAutoSearch;
import com.sc.core.util.DataAutoSearch.SearchDTO;
import com.sc.core.util.RouteScanner;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@AllArgsConstructor
@RequestMapping("admin-base/route")
public class RouteController {

    private final IRouteService routeService;
    private final RouteScanner routeScanner;

    @SneakyThrows
    @GetMapping("list")
    public String list(Model model) {
        String openType = new ObjectMapper().writeValueAsString(RouteEntity.RouteOpenTypeEnum.getEnumList());
        String type = new ObjectMapper().writeValueAsString(RouteEntity.RouteTypeEnum.getEnumList());
        model.addAttribute("openType", openType);
        model.addAttribute("type", type);
        return "base/route/list";
    }

    @GetMapping("list-data")
    @ResponseBody
    public Response<List<RouteListVO>> listData(@RequestParam Map<String, Object> searchDTO) {
        List<RouteListVO> list = routeService.getList(new DataAutoSearch(searchDTO));
        return Response.success(list);
    }

    @SneakyThrows
    @GetMapping("create")
    public String create(Model model) {
        model.addAttribute("openTypes", new ObjectMapper().writeValueAsString(RouteEntity.RouteOpenTypeEnum.getEnumList()));
        model.addAttribute("types", new ObjectMapper().writeValueAsString(RouteEntity.RouteTypeEnum.getEnumList()));
        return "base/route/create";
    }

    @PostMapping("create")
    @ResponseBody
    public Response<RouteEntity> create(@RequestBody RouteEntity routeEntity) {
        routeService.save(routeEntity);
        return Response.success(routeEntity);
    }

    @SneakyThrows
    @GetMapping("update/{id}")
    public String update(@PathVariable Long id, Model model) {
        model.addAttribute("id", id);
        model.addAttribute("openTypes", new ObjectMapper().writeValueAsString(RouteEntity.RouteOpenTypeEnum.getEnumList()));
        model.addAttribute("types", new ObjectMapper().writeValueAsString(RouteEntity.RouteTypeEnum.getEnumList()));
        return "base/route/create";
    }

    @PostMapping("update")
    @ResponseBody
    public Response<RouteEntity> update(@RequestBody RouteEntity routeEntity) {
        routeService.updateById(routeEntity);
        return Response.success(routeEntity);
    }

    @PostMapping("delete")
    @ResponseBody
    public Response<RouteEntity> delete(@RequestBody IdsDTO ids) {
        routeService.removeByIds(ids.getIds());
        return Response.success();
    }

    @GetMapping("get-enum-map")
    @ResponseBody
    public Response<List<Map<String, Object>>> getEnumMap(@RequestParam("name") String name) {
        HashMap<String, List<Map<String, Object>>> enumMap = new HashMap<>();

        enumMap.put("type", RouteEntity.RouteTypeEnum.getEnumList());
        enumMap.put("openType", RouteEntity.RouteOpenTypeEnum.getEnumList());

        return Response.success(enumMap.get(name));
    }

    @GetMapping("get-routes")
    @ResponseBody
    public Response<List<String>> getRoutes() {
        return Response.success(routeScanner.getRoutes());
    }

    @GetMapping("get-info")
    @ResponseBody
    public Response<RouteEntity> getInfo(@RequestParam("id") Long id) {
        return Response.success(routeService.getById(id));
    }
}
